﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 算法基本功.算法._0_数组._1_双指针.额外数学题
{
    internal class 螺旋矩阵1
    {
        //给矩阵 按螺旋顺序输出
        // 函数签名如下
        public List<int> spiralOrder(int[][] matrix)
        {
            //行 列
            int rows = matrix.Length;
            int cols = matrix[0].Length;
            //四个索引边界
            int top = 0;
            int bottom = rows - 1;
            int left = 0;
            int right = cols - 1;

            //结果
            List<int> result = new List<int>();

            while (result.Count < rows * cols)
            {

                //从左到右
                if (top <= bottom)
                {
                    for (int j = left; j <= right; j++)
                    {
                        result.Add(matrix[top][j]);
                    }
                    //上边界下移 以用于下一次的左到右
                    top++;
                }


                //从上到下
                if (left <= right)
                {
                    for (int i = top; i <= bottom; i++)
                    {
                        result.Add(matrix[i][right]);
                    }

                    right--;
                }

                //从右到左
                if (top <= bottom)
                {
                    for (int j = right; j >= left; j--)
                    {
                        result.Add(matrix[bottom][j]);
                    }
                    bottom--;
                }

                //从下到上
                if (left <= right)
                {
                    for (int i = bottom; i >= top; i--)
                    {
                        result.Add(matrix[i][left]);
                    }
                    left++;
                }
            }
            return result;

        }

    }
}
